//
//  receiveFirCirSendNew.m
//  tcpoctest
//
//  Created by gump on 5/9/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "receiveFirCirSendNew.h"

#import "FriendStatus.h"
#import "FirCirLocalInfo.h"

#import "fircirnewsdb.h"
#import "FirendCirLocalPathdb.h"
#import "firendCircledb.h"
#import "FirCirNewsMailDataSource.h"

#import "FirCirNewsDataSource.h"
#import "FirendCircleDataSource.h"
#import "FirCirLocalResourceDataSource.h"
#import "FirCirMailNewsdb.h"
#import "FirCirPersonDataSource.h"

#import "sendFirCirPicture.h"



@implementation receiveFirCirSendNew

-(void)processreceive
{
    //返回 74char(1),1char(1),1char(1)1成功，2发送太多,
    //所创建的动态ID字串长度char(1),所创建的动态ID字串char(x)
    switch ([logicsb ReadChar]) {
        case FC_SENDNEW:
        {
            FriendStatus *fs = [[FriendStatus alloc] init];
            
            fs.mail = [DataControl shareControl].personDS.uinfo.mail;
            fs.statusId = [logicsb ReadUnencodeString];
            fs.strtime = [[DataControl shareControl].fircirDS greatFCcurTimeString];
            fs.stIndex = [[DataControl shareControl].fircirDS getFCSendIndex];
            fs.statusText = [[DataControl shareControl].fircirDS GetFCSendContent];
            
            switch (fs.stIndex) {
                case FCG_ONE_SOUND:
                {
                    FirCirLocalInfo *fclinfo = [[FirCirLocalInfo alloc] init];
                    fclinfo.resourceID = fs.statusId;
                    fclinfo.url = fs.statusText;
                    fclinfo.localpath = fs.statusText;
                    
                    [[DataBaseCtrol shareControl].fircirlocaldatabase insertLocalPath:fclinfo];
                    [[DataControl shareControl].fcLocalRecDS AddLocalRs:fclinfo];
                }
                    break;
                default:
                    break;
            }
            
            [[DataBaseCtrol shareControl].fircirnewdatabase insertNews:fs];
            [[DataControl shareControl].fcnewsDS AddNews:fs];
            
            [[DataControl shareControl].fircirDS AddFirCirArrFirst:fs.statusId];
            [[DataBaseCtrol shareControl].fircirdatabase insertFC:fs.statusId];
            
            [[DataControl shareControl].fcnewsmailDS AddMail:fs.mail withNewsID:fs.statusId];
            [[DataBaseCtrol shareControl].fircirmailnewsdatabase insertNewMail:fs.mail
                                                                      withMail:fs.statusId];
            
            [[DataControl shareControl].fcPersonDS insertNewsIDToMail:fs.mail
                                                           withNewsID:fs.statusId
                                                            withOrder:ORDER_ASC];
            
            if ([[DataControl shareControl].fircirDS getphotoArrCount] == 0) {
                [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_SENDNEWSUCCESS];
            }
            else
            {
                [[DataControl shareControl].fircirDS SetSendPicCount:0];
                UIImage *image = [[DataControl shareControl].fircirDS
                                  getPhotoArrObjectIndex:[[DataControl shareControl].fircirDS SendPicCount]];
                
                if (image != nil) {
                    [[netManager sharedNetManager].sendFCPic SendFCPicture:image withNewsID:fs.statusId];
                    
                    [[DataControl shareControl].fircirDS SetHudString:
                     [NSString stringWithFormat:@"发送图片(%d/%d)",
                      [[DataControl shareControl].fircirDS SendPicCount]
                      ,[[DataControl shareControl].fircirDS getphotoArrCount]]];
                    
                    [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_UPDATESENDHUD];
                }
                
            }
            
            
            
            
            
        }
            break;
        case FC_ADDPICTURE:
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_FIRCIR_SENDNEWFAIL];
        }
            break;
        default:
            break;
    }
    
    [self ShowLog];
}

@end
